package model;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Collections;
public class ListWords {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ArrayList<Word>listwords;
	
	public ListWords() {
		listwords=new ArrayList<Word>();
	}

	@SuppressWarnings("unchecked")
	public ListWords(String fileName) {
		listwords=new ArrayList<Word>(readWords(fileName));
	}
	
	public boolean addWord(Word w) {
		return listwords.add(w);
	}
	
	public boolean removeWord(Word w) {
		return listwords.remove(w);
	}
	
	public int size(){
		return listwords.size();
	}
	
	/**
	 * 
	 * @param index
	 * @return
	 */
	public Word removeWord(int index) {
		return listwords.remove(index);
	}
	
	public Word get(int index) {
		return listwords.get(index);
	}
		
	private int random(int max) {
		Random rand = new Random();
		return rand.nextInt(max);
	}
	/**
	 * 
	 * @return
	 */
	public ArrayList<Word> getListWord(){
		return listwords;
	}
	
	/**
	 * 
	 * @param nbmax
	 * @return
	 */
	public ArrayList<String> getListSyllable(int nbmax){
		ArrayList<String> listSyllable = new ArrayList<String>();
		int nbSyllable=0;
		ArrayList<Word> lw=new ArrayList<Word>(listwords);
		
		while(nbSyllable<nbmax && lw.size()>0) {
			
			int index=random(lw.size());
			Word word=lw.get(index);
			listSyllable.addAll(word.getSyllables());	
			lw.remove(index);
			nbSyllable=listSyllable.size();
			System.out.println(nbSyllable);
		}
		 Collections.shuffle(listSyllable);
		 return listSyllable;
	}
	
	
	private static List<Word> readWords(String name)  {
		 List<Word> lw=new ArrayList<Word>();
		 Word word=null;
		 
		 try{
			 Scanner sc = new Scanner(new File(name));		 
			 while (sc.hasNext()) {
				 String parseWord = sc.next();
				 String words[]=parseWord.split("[:]");
				 word=new Word(words[0],words[1]);
				 lw.add(word);
				 
				 for(int i=2; i<words.length;i++ )
					 word.addSyllable(words[i]);
				 
			 }
		 }catch(FileNotFoundException e) {
			 e.getCause();
		 }
	     return lw;
	}
	
	@SuppressWarnings("unused")
	private static List<Word>  suffleWordList(int nbmax, List<Word> lw) {
		ArrayList<Word> listSuffle = new ArrayList<Word>();
		int nbword=0;
		
		while(nbword<nbmax && lw.size()>0) {
			Random rand = new Random();
			int index=rand.nextInt(lw.size());
		    Word word=lw.get(index);
		    listSuffle.add(word);	
		    lw.remove(index);
			nbword=listSuffle.size();
		}
		 Collections.shuffle(listSuffle);
		 return listSuffle;
	}
	
	
	public static void main(String args[]) {
		ListWords myListe=new ListWords("/home/hady/Bureau/Devint2012/src/model/vehicule.txt"); 
		System.out.println(ListWords.readWords("/home/hady/Bureau/Devint2012/src/model/vehicule.txt"));
		System.out.println(myListe.getListSyllable(8));
	}
}
